#include <mega16.h>

// TXchip pin define
#define MASTER	1

#define PIN_VMON		PINA.0

#define	OUT_COMM_STATUS	PORTB.1			// 0x41		A
//#define IN_PRESET_EN	PINB.2			// 0x42		B
#define IN_SPEED    	PINB.2			// 0x42		B
#define OUT_BTS_EN		PORTB.3			// 0x43		C
#define OUT_LEDMIN_MSTR	PORTB.4			// 0x44		D
#define OUT_LEDMIN_SLV	PORTB.5			// 0x45		E
#define IN_PRESET_3		PINB.7			// 0x46		F

#define IN_ZOOM_P		PINC.0			// 0x47		G
#define IN_ZOOM_M		PINC.1			// 0x48		H
#define IN_FOCUS_P		PINC.2			// 0x49		I
#define IN_FOCUS_M		PINC.3			// 0x4A		J		
#define IN_IRIS_P		PINC.4			// 0x4B		K
#define IN_IRIS_M		PINC.5			// 0x4C		L

#define IN_UP			PIND.2			// 0x4D		M
#define IN_DOWN			PIND.3			// 0x4E		N
#define IN_LEFT			PIND.4			// 0x4F		O
#define IN_RIGHT		PIND.5			// 0x50		P
#define IN_PRESET_1		PIND.6			// 0x51		R
#define IN_PRESET_2		PIND.7			// 0x52		S

// aktivno neaktivno stanje pinova
#define BUTTON_DOWN			0
#define BUTTON_UP			1
#define LED_ON				1
#define LED_OFF				0
#define BTS_ENABLE			1
#define BTS_DISABLE			0

#define VMON_BATT           0
#define VMON_AUX            1

// CMD values
// CMD TX frame	[CMD_HEAD]	[CMD_ID]	[CMD_1]	[CMD_2]	[CHECKSUM]	[CMD_TX_END]
//			 	[0]			[1]			[2]		[3]		[4]			[5]
// CMD RX frame	[CMD_HEAD]	[VOLT_DATA]	[CMD_RX_END]
//			 	[0]			[1]			[2]
#define CMD_ID_IDX		    1
#define CMD_PT_IDX		    2 
#define CMD_ZFI_IDX		    3
#define CMD_CHKSUM_IDX	    4
#define CMD_TX_END_IDX		5		// zadnji index 
#define CMD_RX_END_IDX		2		
#define CMD_HEAD 		0x3C		// <
#define CMD_END 		0x3E		// >


// STATUS values
// pan/tilt
#define m_NONE			0x30	// '0'
#define m_NOKEY			0x31	// '1'

#define m_RIGHT			0x52	// 'R'
#define m_LEFT			0x4C	// 'L'
#define m_UP			0x55	// 'U'
#define m_DOWN			0x44	// 'D'
#define m_BATTERY       'B'

// zoom/focus/iris/preset
#define m_ZOOM_P			0x5A	// 'Z'
#define m_ZOOM_M			0x7A	// 'z'
#define m_FOCUS_P		    0x46	// 'F'
#define m_FOCUS_M		    0x66	// 'f'
#define m_IRIS_P			0x49	// 'I'
#define m_IRIS_M			0x69	// 'i'
#define m_PRESET_SET		'P'
#define m_PRESET_GET		'p'

#define m_SPEED             'S'
//#define m_SPEED_NRM         '0'
//#define m_SPEED_SLW         '1'

// speedState
#define m_SPEED_NONE        '9'
#define m_SPEED_NORM        '0'
#define m_SPEED_SLOW        '1'
#define m_SPEED_SLOW_SENT   '2'
#define m_SPEED_NORM_SENT   '3'


#define m_PRESET_1			0x31	// '1'
#define m_PRESET_2			0x32	// '2'
#define m_PRESET_3			0x33	// '3'

// presetState
#define m_PRESET_NONE       '0'
#define m_PRESET_1_SHORT    'a'
#define m_PRESET_1_LONG     'A'
#define m_PRESET_2_SHORT    'b'
#define m_PRESET_2_LONG     'B'
#define m_PRESET_3_SHORT    'c'
#define m_PRESET_3_LONG     'C'


// status napona baterije
// batSTATE
// bat_SLAVE_STATE
#define BAT_MONITOR_TIME	10	// provjerava se svakih 1s  a prihvaća se stabilna vrijednost od 10s
#define BAT_STATE_HIGH		'0'     //0x30	// defaultna
#define BAT_STATE_MID		'1'     //0x31
#define BAT_STATE_LOW		'2'     //0x32
#define BAT_STATE_OFF		'9'     //0x39
#define BAT_STATE_GET		'Q'     // pita stanje baterije
         
void GetPanTilt();
void GetZoom();
void GetBatteryState();
char CalculateChecksum(char cmd1, char cmd2);